package com.gx.smart.construction.ui.house.viewmodel

import android.app.Application
import com.gx.smart.base.BaseViewModel
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppRoomInfo
import com.gx.smart.construction.ui.house.model.RoomInfo
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask
import com.gx.wisestone.constructionappgrpclib.grpc.roominfo.ConstructionAppGetAllRoomsInfoResponse
import com.casic.gx.grpc.common.ComQuery
import com.gx.smart.lib.http.api.ConstructionAppRoomInfoProviderService
import com.blankj.utilcode.util.ToastUtils
import android.os.AsyncTask
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import androidx.paging.Pager
import androidx.paging.PagingConfig
import com.gx.smart.construction.ui.common.db.ConstructionDatabase
import com.gx.smart.construction.ui.house.paging.RoomInfoBoundaryCallback
import com.gx.smart.construction.ui.house.paging.RoomInfoPagingSource
import com.gx.smart.lib.http.api.asynctask.CallBack

class SmartHomeRecordViewModel(application: Application) : BaseViewModel(application) {
    @JvmField
    var data = MutableLiveData<List<ConstructionAppRoomInfo>>()
    private val selectedType = MutableLiveData("全部")
    @JvmField
    var roomInfoPagedList: LiveData<PagedList<RoomInfo>>
    var roomInfoList =
        Pager(
            config = PagingConfig(
                pageSize = 15,
                enablePlaceholders = false,
                initialLoadSize = 15
            ),
            pagingSourceFactory = {
                RoomInfoPagingSource(3)
            }
        ).flow

    fun getSelectedType(): LiveData<String> {
        return selectedType
    }

    fun setSelectedType(selectedType: String) {
        this.selectedType.value = selectedType
    }

    private var recordListTask: GrpcAsyncTask<String, Void, ConstructionAppGetAllRoomsInfoResponse>? =
        null

    fun recordList(currentPage: Int, sn_is_null: Int) {
        val comQuery = ComQuery.newBuilder()
            .setPage(currentPage)
            .setPageSize(15)
            .build()
        recordListTask = ConstructionAppRoomInfoProviderService.getInstance().getAllRoomsInfo(
            comQuery,
            sn_is_null,
            object : CallBack<ConstructionAppGetAllRoomsInfoResponse>() {
                override fun callBack(result: ConstructionAppGetAllRoomsInfoResponse) {
                    if (result == null) {
                        error.setValue(true)
                        ToastUtils.showLong("服务器超时")
                        return
                    }
                    val comResp = result.comResp
                    if (comResp.code == 100) {
                        data.setValue(result.contentList)
                    } else {
                        error.setValue(true)
                        handleServiceException(comResp)
                    }
                }
            })
    }

    /**
     * 刷新数据
     */
    fun refresh() {
        AsyncTask.execute { ConstructionDatabase.getInstance().roomInfoDao.clearRoomInfo() }
    }

    override fun onCleared() {
        if (recordListTask != null) {
            recordListTask!!.cancelTask(true)
        }
    }

    init {
        val db = ConstructionDatabase.getInstance()
        val pagedConfig: PagedList.Config = PagedList.Config.Builder()
            .setPageSize(15)
            .setPrefetchDistance(15 * 3)
            .setInitialLoadSizeHint(15)
            .setEnablePlaceholders(true)
            .setMaxSize(Int.MAX_VALUE)
            .build()
        roomInfoPagedList =
            LivePagedListBuilder<Int, RoomInfo>(db.roomInfoDao.listRoomInfo(), pagedConfig)
                .setBoundaryCallback(RoomInfoBoundaryCallback())
                .build()
    }
}